<template>
  <div>
    <el-upload :action="FileUrl" ref="eluploadimg" list-type="picture-card" :data="Data" multiple :headers="httpHeaders" :file-list="picList" :auto-upload="true" :on-success="handleAvatarSuccess"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :on-change="handleChange"
      :before-upload="beforeAvatarUpload"
    >
      <i class="el-icon-plus"></i>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt>
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
  name: "ImageUpload",
  props: {
    picList: {
      type: Array,
      default: function() {
        return [];
      }
    },
    Data: {
      type: Object
    },
    FileUrl: {
      type: String
    },
    handleAvatarSuccess: {
      type: Function
    },
    beforeRemove: {
      type: Function
    },
    handleRemove: {
      type: Function
    },
    handleChange: {
      type: Function
    },
    BeforeUpload: {
      type: Function
    }
  },

  data() {
    return {
      httpHeaders: {
        Authorization: getToken()
      },
      dialogImageUrl: "",
      dialogVisible: false
    };
  },
  methods: {
    // 上传前图片校验
    beforeAvatarUpload(file) {
      const isImage = file.type == "image/png" || file.type == "image/jpg" || file.type == "image/jpeg";
      if (!isImage) {
        this.$message.error("上传图片只能是JPG或者png格式!");
      }
      return isImage
    },


    // 上传图片
    UploadSubmit(){
        this.$refs.eluploadimg.submit();
    },
     // 清除图片
    clearFiles(){
        this.$refs.eluploadimg.clearFiles()
    },
  }
};
</script>

